package com.tckdq.tcc.service.impl;

import com.tckdq.tcc.constants.RedisConstant;
import com.tckdq.tcc.exception.GlobalException;
import com.tckdq.tcc.service.ITokenService;
import com.tckdq.tcc.utils.StrUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class TokenServiceImpl implements ITokenService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public String creatToken(String courseId) {
        if (courseId ==null){
            throw new GlobalException("非法请求",null);
        }
        Long longId =3L;


        String tokenKey =RedisConstant.ORDER_TOKEN+longId+courseId;
        String token = StrUtils.getComplexRandomString(6);

        redisTemplate.opsForValue().set(tokenKey,token,10, TimeUnit.MINUTES);

        return token;
    }
}
